package com.jinzhi.jzweb.controller;


import java.util.*;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.jzweb.domain.InfoModelDO;
import com.jinzhi.jzweb.service.InfoModelService;
import com.jinzhi.common.utils.Result;

/**
 * 
 * <pre>
 * 
 * </pre>
 * <small> 2021-02-02 14:44:35 | lance</small>
 */
@Controller
@RequestMapping("/jzweb/infoModel")
public class InfoModelController extends BaseController {
	@Autowired
	private InfoModelService infoModelService;
	
	@GetMapping()
	@RequiresPermissions("jzweb:infoModel:infoModel")
	String InfoModel(Long id,Model model){
		model.addAttribute("infoStoreId",id);
	    return "jzweb/infoModel/infoModel";
	}
	

	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("jzweb:infoModel:infoModel")
	public Result<Page<InfoModelDO>> list(InfoModelDO infoModelDTO){
		Wrapper<InfoModelDO> wrapper = new EntityWrapper<InfoModelDO>().orderBy("sort");
		if(infoModelDTO.getName() != null) {
			wrapper.like("name",infoModelDTO.getName());
		}
		if(infoModelDTO.getType() != null) {
			wrapper.eq("type",infoModelDTO.getType());
		}
		if(infoModelDTO.getInfoStoreId()!=null) {
			wrapper.eq("info_store_id",infoModelDTO.getInfoStoreId());
		}
		// 查询列表数据
		Page<InfoModelDO> page = infoModelService.selectPage(getPage(InfoModelDO.class),wrapper);
		List list = new ArrayList();
		for (int i = 0; i < page.getRecords().size(); i++) {
				InfoModelDO obj = (InfoModelDO) page.getRecords().get(i);
			list.add(obj);
		}
		page.setRecords(list);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("jzweb:infoModel:add")
	String add(Long infoStoreId,Model model){
		model.addAttribute("infoStoreId",infoStoreId);
	    return "jzweb/infoModel/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("jzweb:infoModel:edit")
	String edit(@PathVariable("id") Long id,Model model){
		InfoModelDO infoModel = infoModelService.selectById(id);
		model.addAttribute("infoModel", infoModel);
	    return "jzweb/infoModel/edit";
	}
	
	@Log("添加")
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("jzweb:infoModel:add")
	public Result<String> save( InfoModelDO infoModel){
		infoModelService.insert(infoModel);
        return Result.ok();
	}
	
	@Log("修改")
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("jzweb:infoModel:edit")
	public Result<String>  update( InfoModelDO infoModel){
		boolean update = infoModelService.updateById(infoModel);
		return update ? Result.ok() : Result.fail();
	}
	
	@Log("删除")
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("jzweb:infoModel:remove")
	public Result<String>  remove( Long id){
		infoModelService.deleteById(id);
        return Result.ok();
	}
	
	@Log("批量删除")
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("jzweb:infoModel:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		infoModelService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}
	
}
